/**
 * 
 */
package ru.olympus.antonyhands.ao.server.item.util;

import com.google.common.base.Predicate;

import ru.olympus.antonyhands.ao.server.item.Item;
import ru.olympus.antonyhands.ao.server.item.ItemBase;
import ru.olympus.antonyhands.ao.shared.item.EItemParentType;

/**
 * @author AntonyHands
 * @since 08.03.2012
 */
public class ItemByUniversalParentFilter implements Predicate<Item> {

	private EItemParentType _parentType;
	private String _parentCode;

	public ItemByUniversalParentFilter(EItemParentType parentType, String parentCode)
	{
		_parentType = parentType;
		_parentCode = parentCode;
	}

	@Override
	public boolean apply(Item item) {
		ItemBase parent = getParent(item);
		if (parent == null)
			return false;
		else
			return parent.getCode().equals(_parentCode);
	}

	private ItemBase getParent(Item item)
	{
		if (EItemParentType.ITEM_CATEGORY.equals(_parentType))
			return item.getItemCategory();
		else
			return item.getCraftInfo() == null ? null : item.getCraftInfo().getCraftSkill();
	}
}
